package org.example.service;


import org.example.dto.EmployeeLoginDTO;
import org.example.entity.Employee;
import org.example.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;

@Service
public class EmployeeService {
    @Autowired
    Emplyeemapper emplyeemapper;
    @Autowired
    GowucheService gowucheService;
    @Autowired
    Shouchanmapper shouchanmapper;
    @Autowired
    Dianpumapper dianpumapper;
    @Autowired
    Wdzjmapper wdzjmapper;
    @Autowired
    Kaidianmapper kaidianmapper;
    @Autowired
    Dingdanmapper dingdanmapper;
    @Autowired
    Dizhimapper dizhimapper;
    /**
     * 商家登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {

        return emplyeemapper.delu(employeeLoginDTO);
    }

    /**
     * 注册账户
     *
     * @param employee
     */
    public void loginadd(Employee employee) {
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setImage("http://localhost:8080/image/2.jpg");
        emplyeemapper.zhuche(employee);

    }

    /**
     * 用户查询
     *
     * @param employee
     * @return
     */
    public Integer loginid(Employee employee) {
        return emplyeemapper.id(employee);
    }

    /**
     * 忘记密码
     *
     * @param employee
     * @return
     */
    public void loginwangji(Employee employee) {
        emplyeemapper.wangji(employee);
    }

    /**
     * 查询
     *
     * @param username
     * @return
     */
    public Employee chaxun(String username) {
        return emplyeemapper.chaxun(username);
    }


    /**
     * 修改账号
     *
     * @param avatar
     * @param username
     * @param phone
     * @param currentPassword
     * @param newPassword
     * @return
     */
    public boolean zhanhaoxg(MultipartFile avatar, String username, String phone,
                             String currentPassword, String newPassword, String yonghuusername) {

        String n = avatar.getOriginalFilename();
        String ne = UUID.randomUUID().toString() + n.substring(n.lastIndexOf("."));
        if (avatar.getSize() == 0) {
            // 占位文件，跳过处理
            ne="";
        } else {
            try {
                avatar.transferTo(new File("D:/wenjian/java/image/" + ne));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            ne="http://localhost:8080/image/" + ne;
        }
        if (Objects.equals(currentPassword, "") || currentPassword == null) {
            emplyeemapper.xg(ne,username,phone,newPassword,yonghuusername);
           return true;
        } else {
            EmployeeLoginDTO employeeLoginDTO = new EmployeeLoginDTO();
            employeeLoginDTO.setUsername(yonghuusername);
            employeeLoginDTO.setPassword(currentPassword);
            if (emplyeemapper.delu(employeeLoginDTO) == null) {
                return false;
            } else {
                emplyeemapper.xg(ne,username,phone,newPassword,yonghuusername);
                return true;
            }
        }
    }

    /**
     * 账号查询
     * @param yonghuusername
     * @return
     */
    public Employee cx(String yonghuusername) {
        return emplyeemapper.chaxun(yonghuusername);
    }

    /**
     * 注销
     * @param username
     */
    public void zx(String username) {
        emplyeemapper.sc(username);
        gowucheService.zxsc(username);
        shouchanmapper.zx(username);
        dianpumapper.gzzx(username);
        wdzjmapper.scqb(username);
        kaidianmapper.sc(username);
        dingdanmapper.zxxg(username);
        dingdanmapper.zx(username);
        dizhimapper.zx(username);
    }
}
